package com.example.guiguihomemedical.controller;

import com.example.guiguihomemedical.common.Result;
import com.example.guiguihomemedical.entity.Record;
import com.example.guiguihomemedical.entity.Servicepack;
import com.example.guiguihomemedical.entity.Signing;
import com.example.guiguihomemedical.entity.dto.FamilyarChooseService;
import com.example.guiguihomemedical.entity.dto.RecordList;
import com.example.guiguihomemedical.entity.dto.SigningAdd;
import com.example.guiguihomemedical.entity.dto.Signingdetail;
import com.example.guiguihomemedical.service.SigningService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.lang.reflect.Type;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/signing")
public class SigningController {
    @Autowired
    private SigningService signingService;

    @GetMapping
    public Result getAll(){
        List<Signing> signingList = signingService.getAll();
        return Result.success(signingList);
    }
    @GetMapping("/Signingdetail")
    public Result getSigningdetail(@RequestParam(required = false,value = "signingId") int signingId){
        Signingdetail Signingdetail = signingService.getSigningdetail(signingId);
        return Result.success(Signingdetail);
    }
    @GetMapping("/state")
    public Result getAllstate(@RequestParam(required = false,value = "IdCard") String IdCard,@RequestParam(required = false,value = "StaTime") String StaTime,@RequestParam(required = false, value = "EndTime") String EndTime) throws ParseException {
        List<Signing> signingList = signingService.getAllstate(IdCard,StaTime,EndTime);
        return Result.success(signingList);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id){
        signingService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/trueId/{id}")
    public Result deleteTrueId(@PathVariable int id){
        signingService.deleteTrueId(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Signing signing){
        signingService.add(signing);
        return Result.success();
    }

    @PostMapping("/add")
    public Result addAll(MultipartHttpServletRequest request) throws ParseException {
        String signingAddJson = request.getParameter("signingAdd");
        String packageResponseListJson = request.getParameter("packageResponseList");
        SigningAdd signingAdd = new Gson().fromJson(signingAddJson, SigningAdd.class);
        Type servicepackListType = new TypeToken<List<Servicepack>>() {}.getType();
        List<Servicepack> packageResponseList = new Gson().fromJson(packageResponseListJson, servicepackListType);
        MultipartFile uploadFile = request.getFile("file");
        signingService.addAll(signingAdd, uploadFile,packageResponseList);
        return Result.success();
    }

    @PostMapping("/addFamilySigning")
    public Result addFamilySigningAll(MultipartHttpServletRequest request) throws ParseException {
        String FamilyarChooseServiceJson = request.getParameter("FamilyarChooseService");
        String StaTime = request.getParameter("StaTime");
        String EndTime = request.getParameter("EndTime");
        String doctorId = request.getParameter("doctorId");
        Type servicepackListType = new TypeToken<List<FamilyarChooseService>>() {}.getType();
        List<FamilyarChooseService> familyarChooseServiceList = new Gson().fromJson(FamilyarChooseServiceJson, servicepackListType);
        MultipartFile uploadFile = request.getFile("file");
        signingService.addFamilySigningAll(uploadFile,familyarChooseServiceList,StaTime,EndTime,doctorId);
        return Result.success();
    }
    @PostMapping("/SearchFamilySigning")
    public Result SearchFamilySigningAll(@RequestParam(required = false, value = "familyarchooseserviceListJson") String familyarchooseserviceListJson , @RequestParam(required = false, value = "StaTime") String StaTime,@RequestParam(required = false, value = "EndTime")String EndTime) throws ParseException {
        Type servicepackListType = new TypeToken<List<FamilyarChooseService>>() {}.getType();
        List<FamilyarChooseService> familyarChooseServiceList = new Gson().fromJson(familyarchooseserviceListJson, servicepackListType);
        List<List<RecordList>> recordLists = signingService.SearchFamilySigningAll(familyarChooseServiceList,StaTime,EndTime);
        return Result.success(recordLists);
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id){
        Signing signing = signingService.getById(id);
        return Result.success(signing);
    }
    @PutMapping
    public Result update(@RequestBody Signing signing){
        signingService.update(signing);
        return Result.success();
    }
    @GetMapping("/getNowServering/idCard")
    public Result getByNowServingById(@RequestParam(required = false, value = "IdCard") String IdCard){
        return Result.success(signingService.getByIdcardNowServing(IdCard));
    }
    @GetMapping("/getNowServering")
    public Result getByNowServing(){
        return Result.success(signingService.getRecordByNowServing());
    }

    @GetMapping ("/searchNowServering")
    public Result searchByNowServing(@RequestParam(required = false,value = "searchString") String searchString,@RequestParam(required = false, value = "PageBegin") Integer PageBegin,
                                     @RequestParam(required = false, value = "PageSize") Integer PageSize){

        return Result.success(signingService.searchRecordByNowServing(PageBegin,PageSize,searchString));
    }
    @GetMapping("/endtime/{endId}")
    public Result getByIdOrserByendtime(@PathVariable String endId){
        Signing signing = signingService.getByIdOrserByendtime(endId);
        return Result.success(signing);
    }
}
